﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 删除命令。
    /// ★①，修改类名。
    /// </summary>
    public static class DeleteCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static DeleteCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("DeleteCommand", "DeleteCommand",
                typeof(DeleteCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.Delete,ModifierKeys.None,"Delete")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                DeleteTitleImage();
            }
            else if (Globals.MainWindow.SelectedRelation != null)
            {
                DeleteRelation();
            }
            else if (Globals.MainWindow.SelectedComment != null)
            {
                DeleteComment();
            }
            else if (Globals.MainWindow.SelectedRelation != null)
            {
                DeleteRelation();
            }
            else if (Globals.MainWindow.SelectedPictureBox != null)
            {
                DeletePictureBox();
            }
            else
            {
                DeleteTitle();
            }
        }

        internal static void DeletePictureBox(bool copyToClipboard = false)
        {
            if (Globals.MainWindow.SelectedPictureBox == null) return;

            PictureBox curPb = Globals.MainWindow.SelectedPictureBox;

            ModifingItem mi = new ModifingItem() { ModifingName = "删除图片框" };

            Action actDeletePictureBox = new Action(curPb.ID, -1, curPb.GetType().Name,
                "DeletePictureBox", curPb.XmlData.OuterXml, null,
                ActionType.DeletePictureBox, curPb.MasterTitleID);

            XmlNode pbSet = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("PictureBoxSet");
            if (pbSet != null)
            {
                pbSet.RemoveChild(curPb.XmlData);
            }

            Globals.MainWindow.mainCanvas.Children.Remove(curPb);
            Globals.MainWindow.mainCanvas.Children.Remove(curPb.ConnectLine);
            if (Globals.MainWindow.SelectedPictureBox == curPb)
            {
                Globals.MainWindow.SelectedPictureBox = null;
            }

            mi.Add(actDeletePictureBox);

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.SelectedComment = null;

            if (copyToClipboard)
            {
                Clipboard.SetData(DataFormats.UnicodeText,
                    "<LunarMind.PictureBox>" +
                    curPb.XmlData.OuterXml +
                    "</LunarMind.PictureBox>");
            }
        }

        internal static void DeleteComment(bool copyToClipboard = false)
        {
            if (Globals.MainWindow.SelectedComment == null) return;

            Comment curComment = Globals.MainWindow.SelectedComment;

            ModifingItem mi = new ModifingItem() { ModifingName = "删除备注文本块" };

            Action actDeleteComment = new Action(curComment.ID, -1, curComment.GetType().Name,
                "DeleteComment", curComment.XmlData.OuterXml, null,
                ActionType.DeleteComment, curComment.MasterTitleID);

            XmlNode commentSet = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("CommentSet");
            if (commentSet != null)
            {
                commentSet.RemoveChild(curComment.XmlData);
            }

            Globals.MainWindow.mainCanvas.Children.Remove(curComment);
            Globals.MainWindow.mainCanvas.Children.Remove(curComment.ConnectLine);
            if (Globals.MainWindow.SelectedComment == curComment)
            {
                Globals.MainWindow.SelectedComment = null;
            }

            mi.Add(actDeleteComment);

            if (curComment.IsSummary)
            {
                if (curComment.MasterTitle != null)
                {
                    curComment.MasterTitle.SetSummaryCommentToNull();
                }
                mi.IsNeedRefreshTitleAndLinesLocation = true;
                Globals.MainWindow.RefreshTitleAndLinesLocation();
            }
            else
            {
                mi.IsNeedRefreshTitleAndLinesLocation = false;
            }

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.SelectedComment = null;

            if (copyToClipboard)
            {
                Clipboard.SetData(DataFormats.UnicodeText,
                    "<LunarMind.Comment>" +
                    curComment.XmlData.OuterXml +
                    "</LunarMind.Comment>");
            }
        }

        internal static void DeleteRelation(bool copyToClipboard = false)
        {
            if (Globals.MainWindow.SelectedRelation == null) return;

            Relation curRelation = Globals.MainWindow.SelectedRelation;

            ModifingItem mi = new ModifingItem() { ModifingName = "删除关系线" };

            Action actDeleteRelation = new Action(curRelation.ID, -1, curRelation.GetType().Name,
                "DeleteRelation", curRelation.XmlData.OuterXml, curRelation.StartTitleID,
                ActionType.DeleteRelation, curRelation.EndTitleID);

            XmlNode relationSet = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("RelationSet");
            if (relationSet != null)
            {
                relationSet.RemoveChild(curRelation.XmlData);
            }

            Globals.MainWindow.mainCanvas.Children.Remove(curRelation);
            if (Globals.MainWindow.SelectedRelation == curRelation)
            {
                Globals.MainWindow.SelectedRelation = null;
            }

            mi.Add(actDeleteRelation);

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.SelectedRelation = null;

            if (copyToClipboard)
            {
                Clipboard.SetData(DataFormats.UnicodeText,
                    "<LunarMind.Relation>" +
                    curRelation.XmlData.OuterXml +
                    "</LunarMind.Relation>");
            }
        }

        internal static void DeleteTitleImage(bool copyToClipboard = false)
        {
            if (Globals.MainWindow.SelectedTitleImage == null) return;

            Title masterTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
            if (masterTitle == null) return;

            TitleImage ti = masterTitle.GetTitleImageByID(Globals.MainWindow.SelectedTitleImage.ID);
            if (ti == null || ti != Globals.MainWindow.SelectedTitleImage) return;  //说明出了错。竟然会不在此标题中。

            XmlNode imgSet = masterTitle.XmlData.SelectSingleNode("ImageSet");
            if (imgSet == null) return;

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "删除标题图像"
            };

            Action actDelImg = new Action(ti.ID, ti.Index, ti.GetType().Name, "DeleteTitleImage",
                ti.XmlData.OuterXml, null, ActionType.DeleteTitleImage, masterTitle.ID);

            imgSet.RemoveChild(ti.XmlData);
            masterTitle.ImgStackPanel.Children.Remove(ti);

            mi.Add(actDelImg);

            masterTitle.SelectOnlySelf();
            mi.NewSelectedTitleID = masterTitle.ID;

            ti.Editor.Do(mi);
            ti.Editor.IsModified = true;
            ti.Editor.RefreshTitleAndLinesLocation();

            if (copyToClipboard)
            {
                Clipboard.SetData(DataFormats.UnicodeText,
                    "<LunarMind.TitleImage>" +
                    ti.XmlData.OuterXml +
                    "</LunarMind.TitleImage>");
            }
        }

        /// <summary>
        /// 此方法可能由Cut命令调用。
        /// </summary>
        /// <param name="copyToClipBoard">复制到剪贴板。</param>
        internal static void DeleteTitle(bool copyToClipBoard = false)
        {
            #region 检查是否同一阶（因为可能在其它地方调用，所以光靠CanExecute检测是不行的。

            if (Globals.MainWindow.SelectedTitlesList.Contains(Globals.MainWindow.RootTitle))
            {
                MessageBox.Show("根标题不允许进行[剪切、复制、粘贴、删除]等操作。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            bool sameLevel = false;

            if (Globals.MainWindow.SelectedTitlesList.Count == 1)
            {
                sameLevel = true;
            }
            else
            {
                Title fstTitle = Globals.MainWindow.SelectedTitlesList[0];

                bool sameHigerID = true;

                for (int i = 1; i < Globals.MainWindow.SelectedTitlesList.Count; i++)
                {
                    Title t = Globals.MainWindow.SelectedTitlesList[i];
                    if (t.HigherLevelTitleID != fstTitle.HigherLevelTitleID)
                    {
                        sameHigerID = false; break;
                    }
                }

                sameLevel = sameHigerID;//只允许同阶剪切。
            }
            #endregion

            if (sameLevel == false)
            {
                MessageBox.Show("[剪切、删除、复制、粘贴]这几个操作在选取多个标题时，这些标题必须是同一级。",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Globals.MainWindow.SelectedTitlesList.Sort(Title.titleIndexSortter);//按Index排序。

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "删除标题"
            };

            Title mainSelectedTitle = null;
            foreach (Title t in Globals.SelectedTitlesList)
            {
                if (t.IsMainSelectedTitle)
                {
                    mainSelectedTitle = t; break;
                }
            }
            if (mainSelectedTitle != null)
            {
                mi.OldSelectedTitleID = mainSelectedTitle.ID;
            }

            StringBuilder sbTitles = null;
            StringBuilder sbComments = null;
            StringBuilder sbPictureBoxes = null;

            if (copyToClipBoard)
            {
                sbTitles = new StringBuilder();
                sbTitles.Append("<Titles>");

                sbComments = new StringBuilder();
                sbComments.Append("<Comments>");

                sbPictureBoxes = new StringBuilder();
                sbPictureBoxes.Append("<PictureBoxes>");
            }

            List<Relation> relations = new List<Relation>();
            List<Comment> comments = new List<Comment>();
            List<PictureBox> pictureBoxes = new List<PictureBox>();

            for (int i = Globals.MainWindow.SelectedTitlesList.Count - 1; i >= 0; i--)
            {
                DeleteTitle(Globals.MainWindow.SelectedTitlesList[i], mi, relations, comments,
                    pictureBoxes, copyToClipBoard, sbTitles);
            }

            //全删除之后再删除相关关系？
            XmlNode relationSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("RelationSet");
            if (relationSetNode != null && relations.Count > 0)
            {
                foreach (Relation r in relations)
                {
                    if (Globals.MainWindow.mainCanvas.Children.Contains(r))
                    {
                        Action actDeleteRelation = new Action(r.ID, -1, r.GetType().Name, "DeleteRelation",
                            r.XmlData.OuterXml, r.StartTitleID, ActionType.DeleteRelation, r.EndTitleID);

                        relationSetNode.RemoveChild(r.XmlData);

                        Globals.MainWindow.mainCanvas.Children.Remove(r);

                        mi.Insert(0, actDeleteRelation);//这里不能用Add，否则撤销时顺序会出问题。
                    }
                }
            }

            //再删除所有相关的Comment.
            XmlNode commentSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("CommentSet");
            if (commentSetNode != null && comments.Count > 0)
            {
                foreach (Comment c in comments)
                {
                    Action actdeleteComment = new Action(c.ID, -1, c.GetType().Name,
                            "DeleteComment", c.XmlData.OuterXml, null,
                            ActionType.DeleteComment, c.MasterTitleID);

                    if (Globals.MainWindow.mainCanvas.Children.Contains(c))
                    {
                        Globals.MainWindow.mainCanvas.Children.Remove(c);
                    }

                    if (Globals.MainWindow.mainCanvas.Children.Contains(c.ConnectLine))
                    {
                        Globals.MainWindow.mainCanvas.Children.Remove(c.ConnectLine);
                    }

                    mi.Insert(0, actdeleteComment);//这里不能用Add，否则撤销时顺序会出问题。
                }
            }

            XmlNode pictureBoxSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("PictureBoxSet");

            if (pictureBoxSetNode != null && pictureBoxes.Count > 0)
            {
                foreach (PictureBox p in pictureBoxes)
                {
                    if (Globals.MainWindow.mainCanvas.Children.Contains(p))
                    {
                        Action actDeletePictureBox = new Action(p.ID, -1, p.GetType().Name, "DeletePictureBox",
                           p.XmlData.OuterXml, null, ActionType.DeletePictureBox, p.MasterTitleID);

                        pictureBoxSetNode.RemoveChild(p.XmlData);

                        Globals.MainWindow.mainCanvas.Children.Remove(p);
                        if (p.ConnectLine != null && Globals.MainWindow.mainCanvas.Children.Contains(p.ConnectLine))
                        {
                            Globals.MainWindow.mainCanvas.Children.Remove(p.ConnectLine);
                        }

                        mi.Insert(0, actDeletePictureBox);
                    }
                }
            }

            if (copyToClipBoard)
            {
                if (commentSetNode != null)
                {
                    foreach (Comment c in comments)
                    {
                        if (Globals.MainWindow.mainCanvas.Children.Contains(c))
                        {
                            Action actDeleteComment = new Action(c.ID, -1, c.GetType().Name, "DeleteComment",
                                c.XmlData.OuterXml, null, ActionType.DeleteComment, c.MasterTitleID);

                            commentSetNode.RemoveChild(c.XmlData);

                            Globals.MainWindow.mainCanvas.Children.Remove(c);
                            if (c.ConnectLine != null && Globals.MainWindow.mainCanvas.Children.Contains(c.ConnectLine))
                            {
                                Globals.MainWindow.mainCanvas.Children.Remove(c.ConnectLine);
                            }

                            mi.Insert(0, actDeleteComment);

                            sbComments.Append(c.XmlData.OuterXml);
                        }
                    }
                }

                sbComments.Append("</Comments>");

                if (pictureBoxSetNode != null)
                {
                    foreach (PictureBox p in pictureBoxes)
                    {
                        if (Globals.MainWindow.mainCanvas.Children.Contains(p))
                        {
                            Action actDeletePictureBox = new Action(p.ID, -1, p.GetType().Name, "DeletePictureBox",
                               p.XmlData.OuterXml, null, ActionType.DeletePictureBox, p.MasterTitleID);

                            pictureBoxSetNode.RemoveChild(p.XmlData);

                            Globals.MainWindow.mainCanvas.Children.Remove(p);
                            if (p.ConnectLine != null && Globals.MainWindow.mainCanvas.Children.Contains(p.ConnectLine))
                            {
                                Globals.MainWindow.mainCanvas.Children.Remove(p.ConnectLine);
                            }

                            mi.Insert(0, actDeletePictureBox);

                            sbPictureBoxes.Append(p.XmlData.OuterXml);
                        }
                    }
                }

                sbPictureBoxes.Append("</PictureBoxes>");
            }

            if (copyToClipBoard && sbTitles != null)
            {
                sbTitles.Append("</Titles>");

                Clipboard.SetData(DataFormats.UnicodeText,
                    "<LunarMind.Copy>" +
                        sbTitles.ToString() +
                        sbComments.ToString() +
                        sbPictureBoxes.ToString() +
                    "</LunarMind.Copy>");
            }

            if (mainSelectedTitle != null)
            {
                Title higher = mainSelectedTitle.GetHigherLvelTitle();
                if (higher != null)
                {
                    List<Title> brothers = higher.GetLowerLevelTitles();//删除自身以后，尝试选取

                    Title nextBrother = null;

                    if (brothers.Count == 0)
                    {
                        nextBrother = null;
                    }
                    else if (mainSelectedTitle.Index == brothers.Count)//不减1，因为当前选择的Title已经被remove。
                    {
                        //如果是同级最后一个被删除，选中同级上一个标题。
                        nextBrother = brothers[mainSelectedTitle.Index - 1];
                    }
                    else
                    {
                        if (brothers.Count > 0)
                        {
                            int searchIndex = mainSelectedTitle.Index;
                            if (searchIndex >= brothers.Count) searchIndex = brothers.Count - 1;

                            for (int i = searchIndex; i >= 0; i--)
                            {
                                Title t = brothers[i];
                                if (t.Direction == mainSelectedTitle.Direction && t.Index > mainSelectedTitle.Index)
                                {
                                    //通常情况下，如果同级有标题，尽可能保持选取框位置不变。
                                    //也就是删除当前选取的标题后，同级标题的下一个标题替补到当前位置并被选中。
                                    nextBrother = t; break;
                                }
                            }
                        }
                    }

                    if (nextBrother != null)
                    {
                        mi.NewSelectedTitleID = nextBrother.ID;
                        nextBrother.SelectOnlySelf();
                    }
                    else
                    {
                        mi.NewSelectedTitleID = higher.ID;
                        higher.SelectOnlySelf();
                    }

                    //整个删除完成后，要重新整理剩下来的同级标题的全部索引。
                    List<Title> lowers = higher.GetLowerLevelTitles();
                    lowers.Sort(Title.titleIndexSortter);

                    for (int i = 0; i < lowers.Count; i++)
                    {
                        Title t = lowers[i];
                        if (t.Index != i)
                        {
                            Action actIndex = new Action(t.ID, -1, t.GetType().Name,
                                "Index", t.Index.ToString(), i.ToString());
                            t.Index = i;
                            mi.Add(actIndex);
                        }
                    }
                }
            }

            //Globals.MainWindow.ClearAllFromSelectedTitleList();//这句不能加。因为想在删除后选中某个公式。

            Globals.MainWindow.RefreshTitleAndLinesLocation();
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.Do(mi);
        }

        /// <summary>
        /// 递归删除一个标题及其下所有下级标题。
        /// </summary>
        /// 
        /// <param name="title">从此标题开始删除。</param>
        /// <param name="copyToClipBoard">是否将文本添加到ClipBoard中。
        /// 在此方法内，实际上是将文本添加到参数“sb”中——这是个StringBuilder。</param>
        /// <param name="sb">用来接收准备放到系统剪贴板中的Xml文本数据的缓冲区。</param>
        private static void DeleteTitle(Title title, ModifingItem mi, List<Relation> relations, List<Comment> comments,
             List<PictureBox> pictureBoxes, bool copyToClipBoard, StringBuilder sb)
        {
            if (mi == null || title == null || relations == null) return;

            var higher = title.GetHigherLvelTitle();

            List<Title> lowerTitles = title.GetLowerLevelTitles();

            if (lowerTitles.Count >= 0)
            {
                for (int i = lowerTitles.Count - 1; i >= 0; i--)
                {
                    Title t = lowerTitles[i];
                    DeleteTitle(t, mi, relations, comments, pictureBoxes, copyToClipBoard, sb);
                }
            }

            //删除自身之前，删除与自身有关的任何“关系（Relation）”。            
            foreach (UIElement ue in Globals.MainWindow.mainCanvas.Children)
            {
                Relation r = ue as Relation;
                if (r != null)
                {
                    if (r.StartTitleID == title.ID || r.EndTitleID == title.ID)
                    {
                        if (relations.Contains(r) == false)
                        {
                            relations.Add(r);
                        }
                    }
                }
                else
                {
                    Comment c = ue as Comment;
                    if (c != null)
                    {
                        if (c.MasterTitleID == title.ID)
                        {
                            if (comments.Contains(c) == false)
                            {
                                comments.Add(c);
                            }
                        }
                    }
                    else
                    {
                        PictureBox p = ue as PictureBox;
                        if (p != null)
                        {
                            if (p.MasterTitleID == title.ID)
                            {
                                if (pictureBoxes.Contains(p) == false)
                                {
                                    pictureBoxes.Add(p);
                                }
                            }
                        }
                    }
                }
            }

            XmlNode relationSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("RelationSet");
            XmlNode commentSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("CommentSet");

            //再删除自身。
            Action actDelete = new Action(title.ID, title.Index, title.GetType().Name,
                "DeleteTitle", title.XmlData.OuterXml, null, ActionType.DeleteTitle, title.HigherLevelTitleID);

            Globals.MainWindow.SelectedTitlesList.Clear();//全删除了，自然也不应该被选定。

            if (copyToClipBoard && sb != null)
            {
                //这里要去掉关系线。剪切、复制、粘贴时，不能连关系线一并粘贴，不然就全乱套了。
                if (relationSetNode != null)// || commentSetNode != null)
                {
                    XmlDocument tmpDoc = new XmlDocument();
                    StringBuilder tmpStringBuilder = new StringBuilder();
                    tmpStringBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    tmpStringBuilder.Append(title.XmlData.OuterXml);
                    tmpDoc.LoadXml(tmpStringBuilder.ToString());

                    XmlNode newRelationSetNode = tmpDoc.DocumentElement.SelectSingleNode("RelationSet");
                    if (newRelationSetNode != null)
                    {
                        tmpDoc.DocumentElement.RemoveChild(newRelationSetNode);
                    }

                    //XmlNode newCommentSetNode = tmpDoc.DocumentElement.SelectSingleNode("CommentSet");
                    //if (newCommentSetNode != null)
                    //{
                    //    tmpDoc.DocumentElement.RemoveChild(newCommentSetNode);
                    //}

                    sb.Append(tmpDoc.DocumentElement.OuterXml);
                }
                else
                {
                    sb.Append(title.XmlData.OuterXml);
                }
            }

            XmlNode titleSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("TitleSet");
            if (titleSetNode != null)
            {
                titleSetNode.RemoveChild(title.XmlData);
            }

            Globals.MainWindow.mainCanvas.Children.Remove(title);

            mi.Add(actDelete);

            if (higher != null) higher.IsNeedSizeInvalid = true;
        }


        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.MainWindow.SelectedTitleImage != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedRelation != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedComment != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedPictureBox != null)
            {
                e.CanExecute = true; return;
            }

            if (Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                e.CanExecute = false;
            }
            else
            {
                if (Globals.MainWindow.SelectedTitlesList.Contains(Globals.MainWindow.RootTitle))
                {
                    e.CanExecute = false;//根标题不允许剪切、复制、粘贴、删除。
                    return;
                }

                //检查是否同一阶
                if (Globals.MainWindow.SelectedTitlesList.Count == 1)
                {
                    e.CanExecute = true;
                }
                else
                {
                    Title fstTitle = Globals.MainWindow.SelectedTitlesList[0];

                    bool sameHigerID = true;

                    for (int i = 1; i < Globals.MainWindow.SelectedTitlesList.Count; i++)
                    {
                        Title t = Globals.MainWindow.SelectedTitlesList[i];
                        if (t.HigherLevelTitleID != fstTitle.HigherLevelTitleID)
                        {
                            sameHigerID = false; break;
                        }
                    }

                    e.CanExecute = sameHigerID;//只允许同阶剪切。
                }
            }
        }
    }
}
